package com.ks.core.base;

/**
 * Created by WangLiCheng:nomad21st@163.com:KingDee Co. on 2020/12/20.
 */
public final class Validator {

    public static void throwIfStringIsWhitespace(String value, String parameterName) throws IllegalArgumentException {
        if (!isNullOrEmpty(value) && isNullOrEmpty(value.trim())) {
            throw newArgumentException(parameterName, "WhitespaceOnlyValue");
        }
    }

    public static void throwIfStringIsEmptyOrWhitespace(String value, String parameterName) throws IllegalArgumentException {
        if (value != null && isNullOrEmpty(value.trim())) {
            throw newArgumentException(parameterName, "WhitespaceOnlyValue");
        }
    }

    public static void throwIfStringNullOrEmpty(String value, String parameterName) throws IllegalArgumentException {
        if (isNullOrEmpty(value)) {
            throw newArgumentException(parameterName, "StringNullOrEmpty");
        }
    }

    public static void throwIfArgumentNull(Object argument, String argumentName, String resourceId) throws IllegalArgumentException {
        if (argument == null) {
            throw newNullPointerException(argumentName, resourceId);
        }
    }

    public static void throwArgumentExceptionIf(boolean condition, String argumentName, String resourceId)
            throws IllegalArgumentException {
        if (condition) {
            throw newArgumentException(argumentName, resourceId);
        }
    }

    public static void throwArgumentOutOfRangeIf(boolean condition, String argumentName, String resourceId)
            throws IllegalArgumentException {
        if (condition) {
            throw new IllegalArgumentException(
                    "Argument:" + argumentName + " is out of range, message: " + GetResourceString(resourceId));
        }
    }


    public static IllegalArgumentException newArgumentException(String parameterName, String resourceId) {
        return new IllegalArgumentException(
                "Argument:" + parameterName + " is null, message: " + GetResourceString(resourceId));
    }

    public static IllegalArgumentException newNullPointerException(String argumentName, String resourceId) {
        return new IllegalArgumentException(
                "Argument:" + argumentName + " is null, message: " + GetResourceString(resourceId));
    }

    public static IllegalAccessException newInvalidOperationException(String resourceId) {
        return new IllegalAccessException(GetResourceString(resourceId));
    }

    private static String GetResourceString(String resourceId) {
        return resourceId;
    }

    public static boolean isNullOrEmpty(String value) {
        return value == null || value.length() == 0;
    }

    public static boolean notNullAndEmpty(String value) {
        return !isNullOrEmpty(value);
    }

    public static boolean notNullAndEmpty(String[] values) {
        return values != null && values.length > 0;
    }
}
